package Util;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * @ClassName : HibernateUtil
 * @Author : MagicJava
 * @Date: 2020-12-01 15:18
 * @Description : hibernate工具
 */
public class HibernateUtil {
    private static Session session = null;
    private static SessionFactory sessionFactory = null;


    public Session getHibernateSession() {
        Configuration configuration = new Configuration()
                .configure("hibernate.cfg.xml");
        sessionFactory = configuration.buildSessionFactory();
        this.session = sessionFactory.openSession();
        session.beginTransaction();
        return session;
    }

    public boolean logn(String userName, String password) {
        Query query = session.createQuery("form User u weher 1=1 and u.id=:id and u.name=:nmae");
        query.setString("id", userName);
        query.setString("name", password);
        if (query.list() == null)
            return false;

        return true;
    }

    public void close() {
        this.session.getTransaction().commit();
        this.session.clear();
        this.sessionFactory.close();
    }
}
